package cn.sciento.onelink;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.CloseableHttpClient;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@Data
public abstract class LinkRequest<T extends LinkResponse>{
    public CloseableHttpClient httpClient;
    public HttpRequestBase httpRequestBase;
    public String host;
    public String version;
    public String url = "";
    public String path = "";
    public Method method;
    boolean upload;
    public String contentType;
    protected String token;
    public static enum Method {
        POST,
        GET,
        DELETE,
        PUT;

        private Method() {
        }
    }

    public LinkRequest(Method method) {
        this.method = method;
        switch(method) {
            case POST:
                this.httpRequestBase = new HttpPost();
                break;
            case PUT:
                this.httpRequestBase = new HttpPut();
                break;
            case DELETE:
                this.httpRequestBase = new HttpDelete();
                break;
            default:
                this.httpRequestBase = new HttpGet();
        }

    }

    public void setHeader(Map<String, Object> params) {
        if (params != null) {
            Set<Map.Entry<String, Object>> entries = params.entrySet();
            Iterator var3 = entries.iterator();

            while(var3.hasNext()) {
                Map.Entry<String, Object> entry = (Map.Entry)var3.next();
                this.httpRequestBase.setHeader((String)entry.getKey(), (String)entry.getValue());
            }
        }

    }

    public void setEntity(String json) {
    }

    public void setEntity(Map<String, String> stringMap, Map<String, String> fileMap) {
    }

    public void setCompleteUrl(String url, Map<String, Object> params)  {
        if (params != null) {
            url = url + "?";
            Set<Map.Entry<String, Object>> entries = params.entrySet();
            int size = entries.size();
            int index = 0;
            Iterator var6 = entries.iterator();

            while(var6.hasNext()) {
                Map.Entry<String, Object> entry = (Map.Entry)var6.next();
                if(entry.getKey().equals("appid")){
                    AppManager.appId = entry.getKey();
                }
                url = url + (String)entry.getKey() + "=" + entry.getValue();
                ++index;
                if (index < size) {
                    url = url + "&";
                }
            }
        }
        this.setUrl(this.host+"/"+this.version+url);

        try {
            httpRequestBase.setURI(new URI(this.getUrl()));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public void setHttpMethod(Method method) {
    }

    public void setType(boolean upload) {
        if (!upload) {
            this.httpRequestBase.addHeader("Content-type", "application/x-www-form-urlencoded");
        }

    }
    public  HttpRequestBase generate(){
        return httpRequestBase;
    };
}
